<?php
function is_odd ($num)
{
    if ($num == 49)
    {
        return 0 - 0.0008;
    }
    if ($num % 2 == 1)
    {
        return 1;
    }
    return 2;
}

function is_big ($num)
{
    if ($num == 49)
    {
        return 0 - 0.0008;
    }
    if (24 < $num)
    {
        return 1;
    }
    return 2;
}

function is_last_big ($num)
{
    if ($num == 49)
    {
        return 0 - 0.0008;
    }
    $n = substr($num, 0 - 1);
    if (4 < $n)
    {
        return 1;
    }
    return 2;
}

function is_last_big2 ($num)
{
    if ($num == 25)
    {
        return 0 - 0.0008;
    }
    $p = intval(substr(d2($num), 0, 1)) + intval(substr($num, 0 - 1));
    $n = substr($p, 0 - 1);
    if (4 < $n)
    {
        return 1;
    }
    return 2;
}

function check_chinayear ($num)
{
    global $g_chinayearnum;
    $i = 1;
    while ($i <= 12)
    {
        if (in_array($num, $g_chinayearnum[$i]))
        {
            return $i;
        }
        ++$i;
    }
    return 0 - 0.001;
}

function CalcResult ($betdata, $lottodata, $mod = 1)
{
    global $g_chinayear;
    global $waittime;
    $createtime = $betdata['createtime'];
    $bettype = $betdata['bettype'];
    $numbers = $betdata['numbers'];
    $amount = $betdata['amount'];
    $spec = $lottodata['spec'];
    if ($mod == 1 && $amount != $betdata['unitmoney'])
    {
        return 0 - 0.001;
    }
    switch ($bettype)
    {
        case 0 :
        case 24 :
            $gg = intval($numbers);
            $n = $betdata['rate'];
            if (0 < $gg && $gg < 50 && 1 < $n && $n < 56)
            {
                if ($gg == $lottodata['spec'])
                {
                    return $amount * ( $n - 1 );
                }
                return 0 - $amount;
            }
            return 0 - 0.001;
        case 1 :
            if ($lottodata['p1'] == 0)
            {
                return 0 - 0.0008;
            }
            if ($betdata['rate'] < 1 || 2.6 < $betdata['rate'])
            {
                return 0 - 0.001;
            }
            if (intval($numbers) == $lottodata['p1'])
            {
                return $amount * ( $betdata['rate'] - 1 );
            }
            return 0 - $amount;
        case 2 :
            if ($lottodata['p2'] == 0)
            {
                return 0 - 0.0008;
            }
            if ($betdata['rate'] < 1 || 2.6 < $betdata['rate'])
            {
                return 0 - 0.001;
            }
            if (intval($numbers) == $lottodata['p2'])
            {
                return $amount * ( $betdata['rate'] - 1 );
            }
            return 0 - $amount;
        case 46 :
            $tmp = 32 < $spec ? 1 :( 16 < $spec ? 2 : 3);
            if ($spec == 49)
            {
                return 0 - 0.0008;
            }
            if ($betdata['rate'] < 1 || 3.2 < $betdata['rate'])
            {
                return 0 - 0.001;
            }
            if (intval($numbers) == $tmp)
            {
                return $amount * ( $betdata['rate'] - 1 );
            }
            return 0 - $amount;
        case 47 :
            $tmp = intval(substr(d2($spec), 0, 1)) + intval(substr($spec, 0 - 1));
            $tmp = 6 < $tmp ? 1 : 2;
            if ($spec == 49)
            {
                return 0 - 0.0008;
            }
            if ($betdata['rate'] < 1 || 2.6 < $betdata['rate'])
            {
                return 0 - 0.001;
            }
            if (intval($numbers) == $tmp)
            {
                return $amount * ( $betdata['rate'] - 1 );
            }
            return 0 - $amount;
        case 3 :
            if ($lottodata['p6'] == 0)
            {
                return 0 - 0.0008;
            }
            if ($betdata['rate'] < 1 || 2.6 < $betdata['rate'])
            {
                return 0 - 0.001;
            }
            if (intval($numbers) == $lottodata['p6'])
            {
                return $amount * ( $betdata['rate'] - 1 );
            }
            return 0 - $amount;
        case 4 :
        case 31 :
            $gg = intval($numbers);
            $bingo = false;
            $i = 1;
            while ($i <= 6)
            {
                if (intval($numbers) == $lottodata["n" . $i])
                {
                    $bingo = true;
                }
                ++$i;
            }
            if ($gg < 1 || 49 < $gg || $betdata['rate'] < 1 || 8 < $betdata['rate'])
            {
                return 0 - 0.001;
            }
            if ($bingo)
            {
                return $amount * ( $betdata['rate'] - 1 );
            }
            return 0 - $amount;
        case 5 :
            if ($lottodata['p3'] == 0)
            {
                return 0 - 0.0008;
            }
            if ($betdata['rate'] < 1 || 2.6 < $betdata['rate'])
            {
                return 0 - 0.001;
            }
            if (intval($numbers) == $lottodata['p3'])
            {
                return $amount * ( $betdata['rate'] - 1 );
            }
            return 0 - $amount;
        case 6 :
            if ($lottodata['p4'] == 0)
            {
                return 0 - 0.0008;
            }
            if ($betdata['rate'] < 1 || 2.6 < $betdata['rate'])
            {
                return 0 - 0.001;
            }
            if (intval($numbers) == $lottodata['p4'])
            {
                return $amount * ( $betdata['rate'] - 1 );
            }
            return 0 - $amount;
        case 7 :
            $p = explode("|", $numbers);
            $n = explode(",", $p[3]);
            $len = count($n);
            if ($len != 2)
            {
                return 0 - 0.001;
            }
            $win = 0;
            $err = 0;
            $gg = 1;
            $i = 0;
            while ($i <= $len - 2)
            {
                $j = $i + 1;
                while ($j < $len)
                {
                    if (intval($n[$i]) == intval($n[$j]) || 80 < $p[1])
                    {
                        ++$err;
                    }
                    else
                    {
                        $c = 0;
                        $ii = 1;
                        while ($ii <= 6)
                        {
                            if ($lottodata["n" . $ii] == intval($n[$i]) || $lottodata["n" . $ii] == intval($n[$j]))
                            {
                                ++$c;
                            }
                            ++$ii;
                        }
                        if ($c == 2)
                        {
                            $win += $p[0] * ( $p[1] - 1 );
                        }
                        else
                        {
                            $win -= $p[0];
                        }
                    }
                    ++$j;
                }
                ++$i;
            }
            if (0 < $err)
            {
                $win = 0 - 0.001;
            }
            return $win;
        case 8 : 
            $p = explode("|", $numbers);
            $n = explode(",", $p[3]);
            $len = count($n);
            if ($len != 3)
            {
                return 0 - 0.001;
            }
            $win = 0;
            $err = 0;
            $gg = 1;
            $i = 0;
            while ($i <= $len - 2)
            {
                $j = $i + 1;
                while ($j <= $len - 2)
                {
                    $k = $j + 1;
                    while ($k < $len)
                    {
                        if (intval($n[$i]) == intval($n[$j]) || intval($n[$i]) == intval($n[$k]) || intval($n[$j]) == intval($n[$k]) || 800 < $p[1])
                        {
                            ++$err;
                        }
                        else
                        {
                            $c = 0;
                            $ii = 1;
                            while ($ii <= 6)
                            {
                                if ($lottodata["n" . $ii] == intval($n[$i]) || $lottodata["n" . $ii] == intval($n[$j]) || $lottodata["n" . $ii] == intval($n[$k]))
                                {
                                    ++$c;
                                }
                                ++$ii;
                            }
                            if ($c == 3)
                            {
                                $win += $p[0] * ( $p[1] - 1 );
                            }
                            else
                            {
                                $win -= $p[0];
                            }
                        }
                        ++$k;
                    }
                    ++$j;
                }
                ++$i;
            }
            if (0 < $err)
            {
                $win = 0 - 0.001;
            }
            return $win;
		case 49:
			$_spec=sprintf("%02d",$spec);
			if(intval(substr($_spec,0,1))==intval($numbers))
			{
				$win += $amount * ($betdata['rate'] - 1 );	
			}
			else
				$win=-$amount;
			return $win;
		case 50:
			$_spec=sprintf("%02d",$spec);
			if(intval(substr($_spec,1,1))==intval($numbers))
			{
				$win += $amount * ($betdata['rate'] - 1 );	
			}
			else
				$win=-$amount;
			return $win;
		case 48 :
            $p = explode("|", $numbers);
            $n = explode(",", $p[3]);
            $len = count($n);
            if ($len != 4)
            {
                return 0 - 0.001;
            }
			$c = 0;
            $win = 0;
			for($i=1;$i<=6;$i++)
			{
				if(in_array(sprintf("%02d",$lottodata["n" . $i]),$n))
					$c++;		
			}
			if ($c == 4)
			{
				$win += $p[0] * ( $p[1] - 1 );
			}
			else
			{
				$win -= $p[0];
			}
            return $win;
        case 9 :
        case 33 :
            $p = explode("|", $numbers);
            $n = explode(",", $p[3]);
            $len = count($n);
            if ($len != 3)
            {
                return 0 - 0.001;
            }
            $win = 0;
            $err = 0;
            $gg = 1;
            $r = explode(",", $p[1]);
            $i = 0;
            while ($i <= $len - 2)
            {
                $j = $i + 1;
                while ($j <= $len - 2)
                {
                    $k = $j + 1;
                    while ($k < $len)
                    {
                        if (intval($n[$i]) == intval($n[$j]) || intval($n[$i]) == intval($n[$k]) || intval($n[$j]) == intval($n[$k]) || 90 < $r[0] || 160 < $r[1])
                        {
                            ++$err;
                        }
                        else
                        {
                            $c = 0;
                            $ii = 1;
                            while ($ii <= 6)
                            {
                                if ($lottodata["n" . $ii] == intval($n[$i]) || $lottodata["n" . $ii] == intval($n[$j]) || $lottodata["n" . $ii] == intval($n[$k]))
                                {
                                    ++$c;
                                }
                                ++$ii;
                            }
                            if ($c == 3)
                            {
                                $win += $p[0] * ( $r[1] - 1 );
                            }
                            else if ($c == 2)
                            {
                                $win += $p[0] * ( $r[0] - 1 );
                            }
                            else
                            {
                                $win -= $p[0];
                            }
                        }
                        ++$k;
                    }
                    ++$j;
                }
                ++$i;
            }
            if (0 < $err)
            {
                $win = 0 - 0.001;
            }
            return $win;
        case 10 :
        case 32 :
            $p = explode("|", $numbers);
            $n = explode(",", $p[3]);
            $len = count($n);
            if ($len != 2)
            {
                return 0 - 0.001;
            }
            $win = 0;
            $err = 0;
            $gg = 1;
            $r = explode(",", $p[1]);
            $i = 0;
            while ($i <= $len - 2)
            {
                $j = $i + 1;
                while ($j < $len)
                {
                    if (intval($n[$i]) == intval($n[$j]) || 150 < $r[0] || 150 < $r[1])
                    {
                        ++$err;
                    }
                    else
                    {
                        $c = 0;
                        $c_spec = 0;
                        $ii = 1;
                        while ($ii <= 6)
                        {
                            if ($lottodata["n" . $ii] == intval($n[$i]) || $lottodata["n" . $ii] == intval($n[$j]))
                            {
                                ++$c;
                            }
                            ++$ii;
                        }
                        if ($lottodata['spec'] == intval($n[$i]) || $lottodata['spec'] == intval($n[$j]))
                        {
                            ++$c_spec;
                        }
                        if ($c == 2)
                        {
                            $win += $p[0] * ( $r[1] - 1 );
                        }
                        else if ($c == 1 && $c_spec == 1)
                        {
                            $win += $p[0] * ( $r[0] - 1 );
                        }
                        else
                        {
                            $win -= $p[0];
                        }
                    }
                    ++$j;
                }
                ++$i;
            }
            if (0 < $err)
            {
                $win = 0 - 0.001;
            }
            return $win;
        case 11 :
            $p = explode("|", $numbers);
            $n = explode(",", $p[3]);
            $len = count($n);
            if ($len != 2)
            {
                return 0 - 0.001;
            }
            $win = 0;
            $err = 0;
            $gg = 1;
            $i = 0;
            while ($i <= $len - 2)
            {
                $j = $i + 1;
                while ($j < $len)
                {
                    if (intval($n[$i]) == intval($n[$j]) || 180 < $p[1])
                    {
                        ++$err;
                    }
                    else
                    {
                        $c = 0;
                        $c_spec = 0;
                        $ii = 1;
                        while ($ii <= 6)
                        {
                            if ($lottodata["n" . $ii] == intval($n[$i]) || $lottodata["n" . $ii] == intval($n[$j]))
                            {
                                ++$c;
                            }
                            ++$ii;
                        }
                        if ($lottodata['spec'] == intval($n[$i]) || $lottodata['spec'] == intval($n[$j]))
                        {
                            ++$c_spec;
                        }
                        if ($c == 1 && $c_spec == 1)
                        {
                            $win += $p[0] * ( $p[1] - 1 );
                        }
                        else
                        {
                            $win -= $p[0];
                        }
                    }
                    ++$j;
                }
                ++$i;
            }
            if (0 < $err)
            {
                $win = 0 - 0.001;
            }
            return $win;
        case 29 :
        case 39 :
        case 40 :
        case 41 :
        case 42 :
        case 43 :
        case 44 :
        case 45 :
            $p = explode("|", $numbers);
            $n = explode(",", $p[3]);
            $len = count($n);
            $win = 0;
            $gg = 1;
            $openArr = array();
            $c = 0;
            $qn = $bettype != 29 ? $bettype - 33 : 5;
            $qc = 0;
            if ($len != $qn)
            {
                return 0 - 0.001;
            }
            $i = 0;
            while ($i < $len)
            {
                if (intval($n[$i]) == $qc || intval($n[$i]) < 1 || 49 < intval($n[$i]))
                {
                    return 0 - 0.001;
                }
                $qc = intval($n[$i]);
                ++$i;
            }
            $ii = 1;
            while ($ii <= 6)
            {
                $openArr[] = d2($lottodata["n" . $ii]);
                ++$ii;
            }
            $openArr[] = d2($lottodata['spec']);
            $c = count(array_diff($n, $openArr));
            if ($c == count($n))
            {
                $win += $p[0] * ( $p[1] - 1 );
            }
            else
            {
                $win -= $p[0];
            }
            return $win;
        case 12 :
            $pass = true;
            $chknum = array(0, 0, 0, 0, 0, 0, 0);
            $win = 1;
            $err = 0;
            $p1 = explode(",", $numbers);
            $cn = count($p1);
            $j = 0;
            while ($j < $cn - 1)
            {
                $k = $j + 1;
                while ($k < $cn)
                {
                    $px = explode("_", $p1[$k]);
                    if (1 < strlen($px[0]) || 1 < strlen($px[1]))
                    {
                        return 0 - 0.001;
                    }
                    if (substr($p1[$k], 0, 4) == substr($p1[$j], 0, 4))
                    {
                        return 0 - 0.001;
                    }
                    ++$k;
                }
                ++$j;
            }
            foreach ($p1 as $p2)
            {
                $p3 = explode("_", $p2);
                if ($lottodata["n" . $p3[0]] == 49)
                {
                    return 0 - 0.0008;
                }
                ++$chknum[$p3[0]];
                if (2 < $chknum[$p3[0]])
                {
                    ++$err;
                }
                else
                {
                    switch ($p3[1])
                    {
                        case 1 :
                            if (is_odd($lottodata["n" . $p3[0]]) == 2)
                            {
                                $pass = false;
                            }
                            else if ($p3[2] < 1 || 2.8 < $p3[2])
                            {
                                $pass = false;
                                ++$err;
                            }
                            else
                            {
                                $win *= $p3[2];
                            }
                            break;
                        case 2 :
                            if (is_odd($lottodata["n" . $p3[0]]) == 1)
                            {
                                $pass = false;
                            }
                            else if ($p3[2] < 1 || 2.8 < $p3[2])
                            {
                                $pass = false;
                                ++$err;
                            }
                            else
                            {
                                $win *= $p3[2];
                            }
                            break;
                        case 3 :
                            if (is_big($lottodata["n" . $p3[0]]) == 2)
                            {
                                $pass = false;
                            }
                            else if ($p3[2] < 1 || 2.8 < $p3[2])
                            {
                                $pass = false;
                                ++$err;
                            }
                            else
                            {
                                $win *= $p3[2];
                            }
                            break;
                        case 4 :
                            if (is_big($lottodata["n" . $p3[0]]) == 1)
                            {
                                $pass = false;
                            }
                            else if ($p3[2] < 1 || 2.8 < $p3[2])
                            {
                                $pass = false;
                                ++$err;
                            }
                            else
                            {
                                $win *= $p3[2];
                            }
                            break;
                        case 5 :
                        case 6 :
                        case 7 :
                            if (check_color($lottodata["n" . $p3[0]]) == $p3[1] - 4)
                            {
                                if ($p3[2] < 1 || 4.8 < $p3[2])
                                {
                                    $pass = false;
                                    ++$err;
                                    break;
                                }
                                else
                                {
                                    $win *= $p3[2];
                                }
                            }
                            else
                            {
                                $pass = false;
                            }
                            break;
                    }
                }
            }
            if (0 < $err)
            {
                return 0 - 0.001;
            }
            if ($pass)
            {
                $win = $amount * $win - $amount;
                return min($win, 500000);
            }
            return 0 - $amount;
        case 13 :
            if ($betdata['rate'] < 1 || 3.2 < $betdata['rate'])
            {
                return 0 - 0.001;
            }
            if (intval($numbers) == $lottodata['p7'])
            {
                return $amount * ( $betdata['rate'] - 1 );
            }
            return 0 - $amount;
        case 14 :
            if ($betdata['rate'] < 1 || 14 < $betdata['rate'])
            {
                return 0 - 0.001;
            }
            if (intval($numbers) == $lottodata['p5'])
            {
                return $amount * ( $betdata['rate'] - 1 );
            }
            return 0 - $amount;
        case 15 :
            $p = explode(" ", $numbers);
            $num = is_odd($lottodata["n" . $p[0]]);
            if ($num <= 0)
            {
                return 0 - 0.0008;
            }
            if ($betdata['rate'] < 1 || 2.6 < $betdata['rate'])
            {
                return 0 - 0.001;
            }
            if ($num == $p[1])
            {
                return $amount * ( $betdata['rate'] - 1 );
            }
            return 0 - $amount;
        case 16 :
            $p = explode(" ", $numbers);
            $num = is_big($lottodata["n" . $p[0]]);
            $p[1] = $p[1] == 3 ? 1 : 2;
            if ($num <= 0)
            {
                return 0 - 0.0008;
            }
            if ($betdata['rate'] < 1 || 2.6 < $betdata['rate'])
            {
                return 0 - 0.001;
            }
            if ($num == $p[1])
            {
                return $amount * ( $betdata['rate'] - 1 );
            }
            return 0 - $amount;
        case 17 :
            $p = explode(" ", $numbers);
            if ($betdata['rate'] < 1 || 3.2 < $betdata['rate'])
            {
                return 0 - 0.001;
            }
            if (check_color($lottodata["n" . $p[0]]) == $p[1] - 4)
            {
                return $amount * ( $betdata['rate'] - 1 );
            }
            return 0 - $amount;
        case 18 :
            if ($lottodata['spec'] == 49)
            {
                return 0 - 0.0008;
            }
            $p0 = "";
            $p1 = "";
            switch (intval($numbers))
            {
                case 1 :
                case 2 :
                case 3 :
                case 4 :
                    $p0 = 1;
                    $p1 = $numbers;
                    break;
                case 5 :
                case 6 :
                case 7 :
                case 8 :
                    $p0 = 2;
                    $p1 = $numbers - 4;
                    break;
                case 9 :
                case 10 :
                case 11 :
                case 12 :
                    $p0 = 3;
                    $p1 = $numbers - 8;
                    break;
            }
            if ($betdata['rate'] < 1 || 8 < $betdata['rate'])
            {
                return 0 - 0.001;
            }
            if ($p0 == $lottodata['p7'])
            {
                if ($p1 < 3)
                {
                    if ($p1 == $lottodata['p1'])
                    {
                        return $amount * ( $betdata['rate'] - 1 );
                    }
                    return 0 - $amount;
                }
                if (!( 2 < $p1 ))
                {
                    break;
                }
                else
                {
                    if ($p1 - 2 == $lottodata['p2'])
                    {
                        return $amount * ( $betdata['rate'] - 1 );
                    }
                    return 0 - $amount;
                }
            }
            return 0 - $amount;
        case 19 :
            if ($lottodata['spec'] == 49)
            {
                return 0 - 0.0008;
            }
            $p0 = explode("|", $numbers);
            $p1 = explode("_", $p0[0]);
            $p2 = explode(",", $p0[1]);
            $win = 0;
            $err = 0;
            $cx = intval($p1[1]);
            if (count($p2) != $cx || $cx < 1 || 11 < $cx)
            {
                return 0 - 0.001;
            }
            $j = 0;
            while ($j < $cx - 1)
            {
                $cz = $p2[$j];
                if ($cz == "" || substr($cz, 0, 1) == " " || substr($cz, 1) == " ")
                {
                    ++$err;
                }
                $i = $j + 1;
                while ($i < $cx)
                {
                    if ($p2[$i] == "" || substr($p2[$i], 0, 1) == " " || substr($p2[$i], 1) == " ")
                    {
                        ++$err;
                    }
                    switch ($p2[$i])
                    {
                        case 1 :
                        case 2 :
                        case 3 :
                        case 4 :
                        case 5 :
                        case 6 :
                        case 7 :
                        case 8 :
                        case 9 :
                        case 10 :
                        case 11 :
                        case 12 :
                            if (intval($p2[$i]) == intval($cz))
                            {
                                ++$err;
                            }
                            break;
                        default :
                            ++$err;
                            break;
                    }
                    ++$i;
                }
                ++$j;
            }
            if ($err == 0 && 1 <= $betdata['rate'] && $betdata['rate'] <= 20)
            {
                $i = 0;
                while ($i < $cx)
                {
                    if ($p2[$i] == $lottodata['p5'])
                    {
                        $win = 1;
                    }
                    ++$i;
                }
                if ($p1[0] == 1 && $win == 1)
                {
                    return $amount * ( $betdata['rate'] - 1 );
                }
                if ($p1[0] == 2 && $win == 0)
                {
                    return $amount * ( $betdata['rate'] - 1 );
                }
                return 0 - $amount;
            }
            return 0 - 0.001;
        case 20 :
            if ($lottodata['spec'] == 49)
            {
                return 0 - 0.0008;
            }
            if ($betdata['rate'] < 1 || 2.6 < $betdata['rate'])
            {
                return 0 - 0.001;
            }
            if (intval($numbers) == 1)
            {
                switch ($lottodata['p5'])
                {
                    case 2 :
                    case 7 :
                    case 8 :
                    case 10 :
                    case 11 :
                    case 12 :
                        return $amount * ( $betdata['rate'] - 1 );
                    default :
                        return 0 - $amount;
                }
            }
            else if (intval($numbers) == 2)
            {
                switch ($lottodata['p5'])
                {
                    case 1 :
                    case 3 :
                    case 4 :
                    case 5 :
                    case 6 :
                    case 9 :
                        return $amount * ( $betdata['rate'] - 1 );
                    default :
                        return 0 - $amount;
                }
            }
            else
            {
                break;
            }
        case 21 :
        case 37 :
            $win = 0;
            $i = 1;
            while ($i < 7)
            {
                if ($numbers == check_chinayear($lottodata["n" . $i]))
                {
                    $win = 1;
                }
                ++$i;
            }
            if ($numbers == $lottodata['p5'])
            {
                $win = 1;
            }
            if ($betdata['rate'] < 1 || $bettype == 21 && 4.5 < $betdata['rate'] || $bettype == 37 && 12 < $betdata['rate'])
            {
                return 0 - 0.001;
            }
            if ($win == 1)
            {
                return $amount * ( $betdata['rate'] - 1 );
            }
            return 0 - $amount;
        case 22 :
        case 38 :
            $win = 0;
            $i = 1;
            while ($i < 7)
            {
                if (substr($numbers, 0 - 1) == substr($lottodata["n" . $i], 0 - 1))
                {
                    $win = 1;
                }
                ++$i;
            }
            if (substr($numbers, 0 - 1) == substr($lottodata['spec'], 0 - 1))
            {
                $win = 1;
            }
            if ($betdata['rate'] < 1 || $bettype == 22 && 6 < $betdata['rate'] || $bettype == 38 && 15 < $betdata['rate'])
            {
                return 0 - 0.001;
            }
            if ($win == 1)
            {
                return $amount * ( $betdata['rate'] - 1 );
            }
            return 0 - $amount;
        case 23 :
            $p = explode(" ", $numbers);
            $nums = $lottodata["n" . $p[0]];
            if (9 < $nums && $nums < 49)
            {
                $nums = intval(substr($nums, 0, 1)) + intval(substr($nums, 1, 1));
            }
            $num = is_odd($nums);
            $p[1] = $p[1] == 8 ? 1 : 2;
            if ($num <= 0)
            {
                return 0 - 0.0008;
            }
            if ($betdata['rate'] < 1 || 2.6 < $betdata['rate'])
            {
                return 0 - 0.001;
            }
            if ($num == $p[1])
            {
                return $amount * ( $betdata['rate'] - 1 );
            }
            return 0 - $amount;
        case 25 :
        case 26 :
            $p = explode(" ", $numbers);
            $gg = intval($p[1]);
            $n = $betdata['rate'];
            if (0 < $gg && $gg < 50 && 1 < $n && $n < 56)
            {
                if ($gg == $lottodata["n" . $p[0]])
                {
                    return $amount * ( $n - 1 );
                }
                return 0 - $amount;
            }
            return 0 - 0.001;
        case 27 :
            $err = 0;
            $p1 = explode("|", $numbers);
            $p2 = explode(",", $p1[1]);
            if (count($p2) != $p1[0])
            {
                return 0 - 0.001;
            }
            $j = 0;
            while ($j < $p1[0] - 1)
            {
                $cz = $p2[$j];
                if ($cz == "" || substr($cz, 0, 1) == " " || substr($cz, 1) == " ")
                {
                    ++$err;
                }
                $i = $j + 1;
                while ($i < $p1[0])
                {
                    if ($p2[$i] == "" || substr($p2[$i], 0, 1) == " " || substr($p2[$i], 1) == " ")
                    {
                        ++$err;
                    }
                    switch ($p2[$i])
                    {
                        case 1 :
                        case 2 :
                        case 3 :
                        case 4 :
                        case 5 :
                        case 6 :
                        case 7 :
                        case 8 :
                        case 9 :
                        case 10 :
                        case 11 :
                        case 12 :
                            if (intval($p2[$i]) == intval($cz))
                            {
                                ++$err;
                            }
                            break;
                        default :
                            ++$err;
                            break;
                    }
                    ++$i;
                }
                ++$j;
            }
            if ($p1[0] == 5)
            {
                $ex = 150;
            }
            else if ($p1[0] == 4)
            {
                $ex = 48;
            }
            else if ($p1[0] == 3)
            {
                $ex = 16;
            }
            else
            {
                $ex = 7;
            }
            if ($err == 0 && ( 1 < $betdata['rate'] && $betdata['rate'] < $ex ))
            {
                $w_tot = array();
                $j = 0;
                while ($j < $p1[0])
                {
                    $win = 0;
                    $i = 1;
                    while ($i < 7)
                    {
                        if ($p2[$j] == check_chinayear($lottodata["n" . $i]))
                        {
                            $win = 1;
                        }
                        ++$i;
                    }
                    if ($win == 0 && $p2[$j] == $lottodata['p5'])
                    {
                        $win = 1;
                    }
                    $w_tot[$j] = $win;
                    ++$j;
                }
                $win = 0;
                $j = 0;
                while ($j < $p1[0])
                {
                    if ($w_tot[$j] == 1)
                    {
                        ++$win;
                    }
                    ++$j;
                }
                if ($win == $p1[0])
                {
                    return $amount * ( $betdata['rate'] - 1 );
                }
                return 0 - $amount;
            }
            return 0 - 0.001;
        case 28 :
            if ($lottodata['p2'] == 0)
            {
                return 0 - 0.0008;
            }
            if ($betdata['rate'] < 1 || 2.6 < $betdata['rate'])
            {
                return 0 - 0.001;
            }
            if (intval($numbers) == is_last_big($lottodata['spec']))
            {
                return $amount * ( $betdata['rate'] - 1 );
            }
            return 0 - $amount;
        case 30 :
            if ($lottodata['spec'] == 25)
            {
                return 0 - 0.0008;
            }
            if ($betdata['rate'] < 1 || 2.6 < $betdata['rate'])
            {
                return 0 - 0.001;
            }
            if (intval($numbers) == is_last_big2($lottodata['spec']))
            {
                return $amount * ( $betdata['rate'] - 1 );
            }
            return 0 - $amount;
        case 34 :
        case 35 :
            $ary = array();
            $ary[0] = $lottodata['p5'];
            $win = 1;
            $i = 1;
            while ($i < 7)
            {
                $cc = check_chinayear($lottodata["n" . $i]);
                if (!in_array($cc, $ary))
                {
                    ++$win;
                    $ary[$win] = $cc;
                }
                ++$i;
            }
            if ($bettype == 34)
            {
                if (intval($numbers) == 4 && ( $betdata['rate'] < 1 || 26 < $betdata['rate'] ))
                {
                    return 0 - 0.001;
                }
                if (intval($numbers) == 5 && ( $betdata['rate'] < 1 || 12 < $betdata['rate'] ))
                {
                    return 0 - 0.001;
                }
                if (intval($numbers) == 6 && ( $betdata['rate'] < 1 || 9 < $betdata['rate'] ))
                {
                    return 0 - 0.001;
                }
                if (intval($numbers) == 7 && ( $betdata['rate'] < 1 || 16 < $betdata['rate'] ))
                {
                    return 0 - 0.001;
                }
                if (1 < $win && $win < 5)
                {
                    $win = 4;
                }
                if ($win == intval($numbers))
                {
                    return $amount * ( $betdata['rate'] - 1 );
                }
                return 0 - $amount;
            }
            if ($betdata['rate'] < 1 || 3.8 < $betdata['rate'])
            {
                return 0 - 0.001;
            }
            if (intval($numbers) == is_odd($win))
            {
                return $amount * ( $betdata['rate'] - 1 );
            }
            return 0 - $amount;
        case 36 :
            $err = 0;
            $pp = explode("|", $numbers);
            $p1 = intval(substr($pp[0], 0, 1));
            $p2 = intval(substr($pp[0], 1, 1));
            $p3 = explode(",", $pp[1]);
            if (count($p3) != $p1)
            {
                return 0 - 0.001;
            }
            $j = 0;
            while ($j < $p1 - 1)
            {
                $cz = $p3[$j];
                $i = $j + 1;
                while ($i < $p1)
                {
                    switch ($p3[$i])
                    {
                        case 0 :
                        case 1 :
                        case 2 :
                        case 3 :
                        case 4 :
                        case 5 :
                        case 6 :
                        case 7 :
                        case 8 :
                        case 9 :
                            if (intval($p3[$i]) == intval($cz))
                            {
                                ++$err;
                            }
                            break;
                        default :
                            ++$err;
                            break;
                    }
                    ++$i;
                }
                ++$j;
            }
            if ($p1 == 5)
            {
                $ex = 200;
            }
            else if ($p1 == 4)
            {
                $ex = 60;
            }
            else if ($p1 == 3)
            {
                $ex = 20;
            }
            else
            {
                $ex = 10;
            }
            if (1 < $betdata['rate'] && $betdata['rate'] < $ex)
            {
                $win = 0;
                $j = 0;
                while ($j < $p1)
                {
                    if ($p3[$j] == $lottodata['spec'] % 10)
                    {
                        ++$win;
                    }
                    else
                    {
                        $i = 1;
                        while ($i < 7)
                        {
                            if ($p3[$j] == $lottodata["n" . $i] % 10)
                            {
                                ++$win;
                                break;
                            }
                            ++$i;
                        }
                    }
                    ++$j;
                }
                if ($p2 == 1 && $win == $p1)
                {
                    return $amount * ( $betdata['rate'] - 1 );
                }
                if ($p2 == 2 && $win == 0)
                {
                    return $amount * ( $betdata['rate'] - 1 );
                }
                return 0 - $amount;
            }
            return 0 - 0.001;
        default :
            return 0 - 0.001;
            break;
    }
}

function chkBetPercent ($betdata, $betLvl = 1)
{
    if ($betLvl == 1)
    {
        if ($betdata['mpercent'] < 0)
        {
            return "代理占成异常";
        }
        if ($betdata['percent2'] < 0)
        {
            return "总代占成异常";
        }
        if ($betdata['percent3'] < 0)
        {
            return "股东占成异常";
        }
        if ($betdata['percent4'] < 0)
        {
            return "大股占成异常";
        }
        if ($betdata['upercent'] < 0)
        {
            return "公司占成异常";
        }
        return "合计占成异常";
    }
    if ($betdata['mpercent'] + $betdata['percent2'] + $betdata['percent3'] + $betdata['percent4'] + $betdata['upercent'] != 100 && $betLvl == 2)
    {
        if ($betdata['percent3'] < 0)
        {
            return "走飞单总代占成异常";
        }
        if ($betdata['percent4'] < 0)
        {
            return "走飞单股东占成异常";
        }
        if ($betdata['percent5'] < 0)
        {
            return "走飞单大股占成异常";
        }
        if ($betdata['percent255'] < 0)
        {
            return "走飞单公司占成异常";
        }
        if ($betdata['percent3'] + $betdata['percent4'] + $betdata['percent5'] + $betdata['percent255'] != 100)
        {
            return "走飞单合计占成异常";
        }
    }
    return 0;
}

if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
$ui = new UI();
$id = intval(getvar("id"));
$rs_lo = getdb("!!select * from lotto where id=" . $id);
if ($rs_lo->eof)
{
    $ui->showErrorPage("错误的资料！");
    exit();
}
if ($rs_lo->f['spec'] == 0)
{
    $ui->showErrorPage("还没有设定号码，无法颁奖！");
    exit();
}
;
$conf = new Config();
$checkcalcbet = $conf->get("calcbet");
if (!isset($_GET['mod']) && $checkcalcbet == 1)
{
    $ui->showErrorPage("<p class=error>其他操盘者正在颁奖过帐中，同一时间请勿重复颁奖！</p>");
    exit();
}
$conf->set("calcbet", 1);
$max_limit = intval($conf->get("member_max_limit"));
$fields = array();
$fields['p1'] = is_odd($rs_lo->f['spec']);
$fields['p2'] = is_big($rs_lo->f['spec']);
$fields['total'] = $rs_lo->f['n1'] + $rs_lo->f['n2'] + $rs_lo->f['n3'] + $rs_lo->f['n4'] + $rs_lo->f['n5'] + $rs_lo->f['n6'] + $rs_lo->f['spec'];
$fields['p3'] = $fields['total'] % 2 == 1 ? 1 : 2;
$fields['p4'] = 175 <= $fields['total'] ? 1 : 2;
$fields['p5'] = check_chinayear($rs_lo->f['spec']);
$fields['p7'] = check_color($rs_lo->f['spec']);
if ($rs_lo->f['spec'] == 49)
{
    $fields['p6'] = 0;
}
else
{
    $num = $rs_lo->f['spec'];
    if (9 < $num)
    {
        $num = intval(substr($num, 0, 1)) + intval(substr($num, 1, 1));
    }
    $fields['p6'] = is_odd($num);
}

db_update("lotto", $fields, "id={$id}");
$rs_lo = getdb("!!select * from lotto where id={$id}");
$spec = $rs_lo->f['spec'];
$round = $rs_lo->f['round'];
getdb("update bet set a1=0,b1=0,a2=0,b2=0,c2=0,d2=0,e2=0,a3=0,b3=0,c3=0,d3=0,e3=0,a4=0,b4=0,c4=0,d4=0,e4=0,a5=0,b5=0,c5=0,d5=0,e5=0,r5=0,g5=0,r6=0,rame=0 where lotto_id=" . $id . " and rame in (1,3,9)");
getdb("update bet2 set a2=0,b2=0,a3=0,b3=0,c3=0,d3=0,e3=0,a4=0,b4=0,c4=0,d4=0,e4=0,a5=0,b5=0,c5=0,d5=0,e5=0,r5=0,g5=0,r6=0,rame=0 where lotto_id=" . $id . " and rame in (1,3,9)");
getdb("update bet8 set r5=0,rame=0 where lotto_id=" . $id . " and rame in (1,3,9)");
$minbackbet = $conf->get("minbackbet");
$total_bet = 0;
$waitbet = $conf->get("waitbet");
$waitbet = $rs_lo->f['open'] . " " . substr($waitbet, 0, 2) . ":" . substr($waitbet, 2, 2) . ":59";
$stopbet = $conf->get("stopbet");
$stopbet = $rs_lo->f['open'] . " " . substr($stopbet, 0, 2) . ":" . substr($stopbet, 2, 2) . ":59";

$waittime = strtotime($waitbet);
$stoptime = strtotime($stopbet);
$waittime += 120;
$stoptime += 120;
$opennum = 1;
$userdata=array();
while ($opennum == 1)
{
    $lotto_am = 0;
    $lotto_mb = 0;
    $lotto_ad = 0;
    $lotto_uo = 0;
    $lotto_ui = 0;
    $lotto_gg = 0;
    $rs = getdb("!!select * from bet where lotto_id=" . $id . " and rame in (0,3,9) order by user_id,bettype");
    $i = 0;
    while (!$rs->eof)
    {
        if ($rs->f['createtime'] <= $stoptime)
        {
            if ($rs->f['rame'] != 9)
            {
                $total = $rs->f['amount'];
                $bettype = $rs->f['bettype'];
                $lotto_am += $total;
                $re = CalcResult($rs->f, $rs_lo->f);
                $chkBetPercent = chkBetPercent($rs->f, 1);
                if ($chkBetPercent != 0)
                {
                    $fields = array();
                    $fields['rame'] = 3;
                    db_update("bet", $fields, "id=" . $rs->f['id']);
                    $errdoc = "颁奖时发现异常注单 - " . $chkBetPercent . "（I:" . $rs->f['id'] . " B:" . $bettype . " N:" . $rs->f['numbers'] . " R:" . $rs->f['rate'] . " A:" . $total . "）";
                    write_exceptional_log($rs->f['user_id'], 1, $id, $errdoc);
                }
                else
                {
                    if ($re == 0 - 0.001)
                    {
                        $fields = array();
                        $fields['rame'] = 3;
                        db_update("bet", $fields, "id=" . $rs->f['id']);
                        $errdoc = "颁奖时发现异常注单（I:" . $rs->f['id'] . " B:" . $bettype . " N:" . $rs->f['numbers'] . " R:" . $rs->f['rate'] . " A:" . $total . "）";
                        write_exceptional_log($rs->f['user_id'], 1, $id, $errdoc);
                    }
                    else
                    {
                        if ($re == 0 - 0.0008)
                        {
                            $fields = array();
                            $fields['rame'] = 9;
                            db_update("bet", $fields, "id=" . $rs->f['id']);
                        }
                        else
                        {
                            $r_corp = $rs->f['upercent'];
                            $r_subsidary = $rs->f['percent4'];
                            $r_shareowner = $rs->f['percent3'];
                            $r_genagent = $rs->f['percent2'];
                            $r_agent = $rs->f['mpercent'];
                            $allbackzero = false;
                            if ($total < $minbackbet)
                            {
                                $allbackzero = true;
                            }
                            else if (in_array($bettype, array(7, 8, 9, 10, 11, 29, 32)))
                            {
                                $p = explode("|", $rs->f['numbers']);
                                if ($p[0] < $minbackbet)
                                {
                                    $allbackzero = true;
                                }
                            }
                            $back1 = $allbackzero ? 0 : 100 - $rs->f['back1'];
                            $a1 = $back1 * $total / 100;
                            $b1 = $re + $a1;
                            $back2 = $allbackzero ? 0 : 100 - $rs->f['back2'];
                            $e2 = $re <= 0 ? 0 : $rs->f['amount'] * $rs->f['rate1'];
                            $a2 = max(0, ( $back2 - $back1 ) * $total / 100);
                            $b2 = $b1 + $a2 + $e2;
                            $c2 = $b2 * $r_agent / 100;
                            $d2 = $b2 * ( 100 - $r_agent ) / 100;
                            $back3 = $allbackzero ? 0 : 100 - $rs->f['back3'];
                            $e3 = $re <= 0 ? 0 : $rs->f['amount'] * $rs->f['rate2'];
                            $a3 = max(0, ( $back3 - $back2 ) * $total / 100);
                            $b3 = $b2 + $a3 + $e3;
                            $c3 = $b3 * $r_genagent / 100;
                            $d3 = $b3 * ( 100 - $r_genagent - $r_agent ) / 100;
                            $r2 = $total * ( $r_genagent / 100 );
                            $e3 -= $e3 * $r_agent / 100;
                            $a3 -= $a3 * $r_agent / 100;
                            $back4 = $allbackzero ? 0 : 100 - $rs->f['back4'];
                            $e4 = $re <= 0 ? 0 : $rs->f['amount'] * $rs->f['rate3'];
                            $a4 = max(0, ( $back4 - $back3 ) * $total / 100);
                            $b4 = $b3 + $a4 + $e4;
                            $c4 = $b4 * $r_shareowner / 100;
                            $d4 = $b4 * ( 100 - $r_shareowner - $r_genagent - $r_agent ) / 100;
                            $r3 = $total * ( $r_shareowner / 100 );
                            $e4 -= $e4 * ( $r_genagent + $r_agent ) / 100;
                            $a4 -= $a4 * ( $r_genagent + $r_agent ) / 100;
                            $back5 = $allbackzero ? 0 : 100 - $rs->f['back5'];
                            $e5 = $re <= 0 ? 0 : $rs->f['amount'] * ( $rs->f['rate4'] + $rs->f['rate5'] );
                            $a5 = max(0, ( $back5 - $back4 ) * $total / 100);
                            $b5 = $b4 + $a5 + $e5;
                            $c5 = $b5 * $r_subsidary / 100;
                            $r4 = $total * ( $r_subsidary / 100 );
                            $e5 -= $e5 * ( $r_shareowner + $r_genagent + $r_agent ) / 100;
                            $a5 -= $a5 * ( $r_shareowner + $r_genagent + $r_agent ) / 100;
                            $back6 = $allbackzero ? 0 : $rs->f['back6'];
                            $g5 = max(0, $back6 * ( $total / 100 ) * ( $r_corp / 100 ));
                            $d5 = $b5 * $r_corp / 100;
                            $r5 = 0 - $d5;
                            $r6 = $total * ( $r_corp / 100 );
                            $lotto_mb += $b1;
                            $lotto_ad += $r5;
                            $lotto_gg += $g5;
                            $fields = array();
                            $fields['a1'] = $a1;
                            $fields['b1'] = $b1;
                            $fields['a2'] = $a2;
                            $fields['b2'] = $b2;
                            $fields['c2'] = $c2;
                            $fields['d2'] = $d2;
                            $fields['e2'] = $e2;
                            $fields['r2'] = $r2;
                            $fields['a3'] = $a3;
                            $fields['b3'] = $b3;
                            $fields['c3'] = $c3;
                            $fields['d3'] = $d3;
                            $fields['e3'] = $e3;
                            $fields['r3'] = $r3;
                            $fields['a4'] = $a4;
                            $fields['b4'] = $b4;
                            $fields['c4'] = $c4;
                            $fields['d4'] = $d4;
                            $fields['e4'] = $e4;
                            $fields['r4'] = $r4;
                            $fields['a5'] = $a5;
                            $fields['b5'] = $b5;
                            $fields['c5'] = $c5;
                            $fields['d5'] = $d5;
                            $fields['e5'] = $e5;
                            $fields['g5'] = $g5;
                            $fields['r5'] = $r5;
                            $fields['r6'] = $r6;
                            $fields['rame'] = 9;
                            db_update("bet", $fields, "id=" . $rs->f['id']);
							$total=($b1>0?$b1+$amount:$a1);
							$uid=$rs->f['user_id'];
							if(isset($userdata[$uid]))
							{
								$userdata[$uid]+=$total;
							}
							else
								$userdata[$uid]=$total;
                        }
                    }
                }
            }
        }
        else
        {
            $fields = array();
            $fields['rame'] = 1;
            db_update("bet", $fields, "id=" . $rs->f['id']);
        }
        ++$i;
        $rs->movenext();
    }
    $rs->close();
    $rs = getdb("!!select * from bet2 where lotto_id=" . $id . " and rame in (0,3,9) order by user_id,bettype");
    while (!$rs->eof)
    {
        if ($rs->f['createtime'] <= $stoptime)
        {
            if ($rs->f['rame'] != 9)
            {
                $total = $rs->f['amount'];
                $bettype = $rs->f['bettype'];
                $theacl = $rs->f['acl'];
                $re = CalcResult($rs->f, $rs_lo->f);
                $a2 = 0;
                $b2 = 0;
                $r2 = 0;
                $a3 = 0;
                $b3 = 0;
                $c3 = 0;
                $d3 = 0;
                $e3 = 0;
                $r3 = 0;
                $a4 = 0;
                $b4 = 0;
                $c4 = 0;
                $d4 = 0;
                $e4 = 0;
                $r4 = 0;
                $a5 = 0;
                $b5 = 0;
                $c5 = 0;
                $d5 = 0;
                $e5 = 0;
                $g5 = 0;
                $r5 = 0;
                $r6 = 0;
                $chkBetPercent = chkBetPercent($rs->f, 2);
                if ($chkBetPercent != 0)
                {
                    $fields = array();
                    $fields['rame'] = 3;
                    db_update("bet2", $fields, "id=" . $rs->f['id']);
                    $errdoc = "颁奖时发现异常走飞 - " . $chkBetPercent . "（I:" . $rs->f['id'] . " B:" . $bettype . " N:" . $rs->f['numbers'] . " R:" . $rs->f['rate'] . " A:" . $total . "）";
                    write_exceptional_log($rs->f['user_id'], 1, $id, $errdoc);
                }
                else
                {
                    if ($re == 0 - 0.001)
                    {
                        $fields = array();
                        $fields['rame'] = 3;
                        db_update("bet2", $fields, "id=" . $rs->f['id']);
                        write_exceptional_log($rs->f['user_id'], $theacl, $id, "颁奖时发现异常走飞（I:" . $rs->f['id'] . " B:" . $bettype . " N:" . $rs->f['numbers'] . " R:" . $rs->f['rate'] . " A:" . $total . "）");
                    }
                    else
                    {
                        if ($re == 0 - 0.0008)
                        {
                            $fields = array();
                            $fields['rame'] = 9;
                            db_update("bet2", $fields, "id=" . $rs->f['id']);
                        }
                        else
                        {
                            $allbackzero = false;
                            if ($total < $minbackbet)
                            {
                                $allbackzero = true;
                            }
                            else if (in_array($bettype, array(7, 8, 9, 10, 11, 29, 32)))
                            {
                                $p = explode("|", $rs->f['numbers']);
                                if ($p[0] < $minbackbet)
                                {
                                    $allbackzero = true;
                                }
                            }
                            if ($theacl == 2)
                            {
                                $back2 = $allbackzero ? 0 : 100 - $rs->f['back2'];
                                $back3 = $allbackzero ? 0 : 100 - $rs->f['back3'];
                                $back4 = $allbackzero ? 0 : 100 - $rs->f['back4'];
                                $back5 = $allbackzero ? 0 : 100 - $rs->f['back5'];
                                $r_corp = $rs->f['percent255'];
                                $r_subsidary = $rs->f['percent5'];
                                $r_shareowner = $rs->f['percent4'];
                                $r_genagent = $rs->f['percent3'];
                                $a2 = $back2 * $total / 100;
                                $b2 = $re + $a2;
                                $e3 = $re <= 0 ? 0 : $rs->f['amount'] * $rs->f['rate2'];
                                $a3 = max(0, ( $back3 - $back2 ) * $total / 100);
                                $b3 = $b2 + $a3 + $e3;
                                $c3 = $b3 * $r_genagent / 100;
                                $d3 = $b3 * ( 100 - $r_genagent ) / 100;
                                $r2 = $total * ( $r_genagent / 100 );
                                $e4 = $re <= 0 ? 0 : $rs->f['amount'] * $rs->f['rate3'];
                                $a4 = max(0, ( $back4 - $back3 ) * $total / 100);
                                $b4 = $b3 + $a4 + $e4;
                                $c4 = $b4 * $r_shareowner / 100;
                                $d4 = $b4 * ( 100 - $r_shareowner - $r_genagent ) / 100;
                                $r3 = $total * ( $r_shareowner / 100 );
                                $e4 -= $e4 * $r_genagent / 100;
                                $a4 -= $a4 * $r_genagent / 100;
                                $e5 = $re <= 0 ? 0 : $rs->f['amount'] * ( $rs->f['rate5'] + $rs->f['rate4'] );
                                $a5 = max(0, ( $back5 - $back4 ) * $total / 100);
                                $b5 = $b4 + $a5 + $e5;
                                $c5 = $b5 * $r_subsidary / 100;
                                $r4 = $total * ( $r_subsidary / 100 );
                                $e5 -= $e5 * ( $r_shareowner + $r_genagent ) / 100;
                                $a5 -= $a5 * ( $r_shareowner + $r_genagent ) / 100;
                            }
                            else if ($theacl == 3)
                            {
                                $back3 = $allbackzero ? 0 : 100 - $rs->f['back3'];
                                $back4 = $allbackzero ? 0 : 100 - $rs->f['back4'];
                                $back5 = $allbackzero ? 0 : 100 - $rs->f['back5'];
                                $r_corp = $rs->f['percent255'];
                                $r_subsidary = $rs->f['percent5'];
                                $r_shareowner = $rs->f['percent4'];
                                $a3 = $back3 * $total / 100;
                                $b3 = $re + $a3;
                                $c3 = $b3;
                                $d3 = $c3;
                                $e4 = $re <= 0 ? 0 : $rs->f['amount'] * $rs->f['rate3'];
                                $a4 = max(0, ( $back4 - $back3 ) * $total / 100);
                                $b4 = $b3 + $a4 + $e4;
                                $c4 = $b4 * $r_shareowner / 100;
                                $d4 = $b4 * ( 100 - $r_shareowner ) / 100;
                                $r3 = $total * ( $r_shareowner / 100 );
                                $e5 = $re <= 0 ? 0 : $rs->f['amount'] * ( $rs->f['rate5'] + $rs->f['rate4'] );
                                $a5 = max(0, ( $back5 - $back4 ) * $total / 100);
                                $b5 = $b4 + $a5 + $e5;
                                $c5 = $b5 * $r_subsidary / 100;
                                $r4 = $total * ( $r_subsidary / 100 );
                                $e5 -= $e5 * $r_shareowner / 100;
                                $a5 -= $a5 * $r_shareowner / 100;
                            }
                            else
                            {
                                if ($theacl == 4)
                                {
                                    $back4 = $allbackzero ? 0 : 100 - $rs->f['back4'];
                                    $back5 = $allbackzero ? 0 : 100 - $rs->f['back5'];
                                    $r_corp = $rs->f['percent255'];
                                    $r_subsidary = $rs->f['percent5'];
                                    $a4 = $back4 * $total / 100;
                                    $b4 = $re + $a4;
                                    $c4 = $b4;
                                    $d4 = $c4;
                                    $e5 = $re <= 0 ? 0 : $rs->f['amount'] * ( $rs->f['rate5'] + $rs->f['rate4'] );
                                    $a5 = max(0, ( $back5 - $back4 ) * $total / 100);
                                    $b5 = $b4 + $a5 + $e5;
                                    $c5 = $b5 * $r_subsidary / 100;
                                    $r4 = $total * ( $r_subsidary / 100 );
                                }
                                else
                                {
                                    $back5 = $allbackzero ? 0 : 100 - $rs->f['back5'];
                                    $r_corp = $rs->f['percent255'];
                                    $e5 = $re <= 0 ? 0 : $rs->f['amount'] * $rs->f['rate5'];
                                    $a5 = $back5 * $total / 100;
                                    $b5 = $re + $a5 + $e5;
                                    $c5 = $b5;
                                }
                            }
                            $back6 = $allbackzero ? 0 : $rs->f['back6'];
                            $g5 = max(0, $back6 * ( $total / 100 ) * ( $r_corp / 100 ));
                            $d5 = $b5 * $r_corp / 100;
                            $r5 = 0 - $d5;
                            $r6 = $total * ( $r_corp / 100 );
                            $lotto_ad += $r5;
                            $lotto_gg += $g5;
                            $fields = array();
                            $fields['a2'] = $a2;
                            $fields['b2'] = $b2;
                            $fields['r2'] = $r2;
                            $fields['a3'] = $a3;
                            $fields['b3'] = $b3;
                            $fields['c3'] = $c3;
                            $fields['d3'] = $d3;
                            $fields['e3'] = $e3;
                            $fields['r3'] = $r3;
                            $fields['a4'] = $a4;
                            $fields['b4'] = $b4;
                            $fields['c4'] = $c4;
                            $fields['d4'] = $d4;
                            $fields['e4'] = $e4;
                            $fields['r4'] = $r4;
                            $fields['a5'] = $a5;
                            $fields['b5'] = $b5;
                            $fields['c5'] = $c5;
                            $fields['d5'] = $d5;
                            $fields['e5'] = $e5;
                            $fields['r5'] = $r5;
                            $fields['g5'] = $g5;
                            $fields['r6'] = $r6;
                            $fields['rame'] = 9;
                            db_update("bet2", $fields, "id=" . $rs->f['id']);
                        }
                    }
                }
            }
        }
        else
        {
            $fields = array();
            $fields['rame'] = 1;
            db_update("bet2", $fields, "id=" . $rs->f['id']);
        }
        ++$i;
        $rs->movenext();
    }
    $rs->close();
    $rs = getdb("!!select * from bet8 where lotto_id=" . $id . " and rame in (0,9) order by bettype");
    while (!$rs->eof)
    {
        if ($rs->f['rame'] != 9)
        {
            $total = $rs->f['amount'];
            $back = $rs->f['back'];
            $bettype = $rs->f['bettype'];
            $lotto_uo += $total;
            $re = CalcResult($rs->f, $rs_lo->f, 0);
            if ($re == 0 - 0.001)
            {
                $fields = array();
                $fields['rame'] = 3;
                db_update("bet8", $fields, "id=" . $rs->f['id']);
                write_exceptional_log($curruser['id'], 255, $id, "颁奖时发现错误走飞（I:" . $rs->f['id'] . " B:" . $bettype . " N:" . $rs->f['numbers'] . " R:" . $rs->f['rate'] . "@" . $back . " A:" . $total . "）");
            }
            else if ($re == 0 - 0.0008)
            {
                $fields = array();
                $fields['rame'] = 9;
                db_update("bet8", $fields, "id=" . $rs->f['id']);
            }
            else
            {
                $r5 = $re + $back * $total / 100;
                $lotto_ui += $r5;
                $fields = array();
                $fields['r5'] = $r5;
                $fields['rame'] = 9;
                db_update("bet8", $fields, "id=" . $rs->f['id']);
            }
        }
        $rs->movenext();
    }
    $rs->close();
    $opennum = 0;
    $rs = getdb("!!select id from bet where lotto_id=" . $id . " and rame=0");
    if (!$rs->eof)
    {
        $opennum = 1;
    }
    $rs->close();
    $rs = getdb("!!select id from bet2 where lotto_id=" . $id . " and rame=0");
    if (!$rs->eof)
    {
        $opennum = 1;
    }
    $rs->close();
    $rs = getdb("!!select id from bet8 where lotto_id=" . $id . " and rame=0");
    if (!$rs->eof)
    {
        $opennum = 1;
    }
    $rs->close();
}
foreach($userdata as $key=>$money)
	UpBalance(0,$id,$key,$money);
unset($userdata);
		
$lotto_ad -= $lotto_gg;
$lotto_sum = $lotto_ad + $lotto_ui;

getdb("update lotto set amount=" . $lotto_am . ",mb=" . $lotto_mb . ",ad=" . $lotto_ad . ",ua=" . $lotto_uo . ",ur=" . $lotto_ui . ",rsum=" . $lotto_sum . " where id=" . $id);
echo "<p>期数：" . d3($round) . "&#12288;&#12288;特码：" . $spec . "&#12288;&#12288;<font color=red>（请仔细检查是否过帐正确，再选择重新颁奖或离开）</font></p>";
echo "<table width=758 border=1 cellpadding=3 cellspacing=0>";
echo "<tr class=td1 align=center nowrap><td nowrap>下注种类<td nowrap>笔数<td nowrap>下注金额";
echo "<td nowrap>会员<td nowrap>公司<td nowrap>收飞金额<td nowrap>收飞结果<td nowrap>总监退水<td nowrap>公司合计";
//$kk = array();
//$i = 0;
//while ($i < count($g_betname))
//{
//    $kk[$i] = $i;
//    ++$i;
//}
$total_count = 0;
$total_amount = 0;
$sum_result1 = 0;
$sum_result4 = 0;
$sum_result5 = 0;
$sum_result6 = 0;
$sum_result7 = 0;
$sum_result8 = 0;
$sum_result9 = 0;
$sum_result0 = 0;
$sum_resultG = 0;
foreach ($g_betname as $k=>$val)
{
    $sql = "!!select sum(amount),sum(b1),sum(r5),sum(c5),count(amount),sum(r6),sum(g5) from bet where lotto_id=" . $id . " and bettype=" . $k . " and rame=9";
    $rs2 = getdb($sql);
    $this_count = $rs2->f[4];
    $amount = 0;
    $result1 = 0;
    $result5 = 0;
    $result4 = 0;
    $result6 = 0;
    $result7 = 0;
    $result8 = 0;
    $result9 = 0;
    $result0 = 0;
    $resultG = 0;
    if (0 < $this_count)
    {
        $amount = $rs2->f[0];
        $result1 = $rs2->f[1];
        $result5 = $rs2->f[2];
        $result4 = 0 - $rs2->f[3];
        $result6 = $rs2->f[5];
        $resultG = $rs2->f[6];
    }
    echo "<tr align=right class=td2 onmouseover=msover() onmouseout=msout()>";
    echo "<td width='11%' align=left nowrap>" . $val;
    echo "<td nowrap>" . $this_count;
    echo "<td nowrap>" . $amount;
    $total_count += $this_count;
    $result8 = 0;
    $result9 = 0;
    $sql = "!!select sum(amount*percent255/100),sum(r5),sum(r6),sum(g5) from bet2 where lotto_id=" . $id . " and bettype=" . $k . " and rame=9";
    $rsb = getdb($sql);
    if (0 < $rsb->f[0])
    {
        $result8 = $rsb->f[0];
        $result9 = $rsb->f[1];
        $result6 += $rsb->f[2];
        $resultG += $rsb->f[3];
    }
    if (0 < $sum_result6)
    {
        $result7 = $result6 / $sum_result6 * 100;
    }
    echo "<td nowrap>" . say_numbercolor($result1);
    echo "<td nowrap>" . say_numbercolor($result5);
    echo "<td nowrap>" . say_numbercolor($result8);
    echo "<td nowrap>" . say_numbercolor($result9);
    echo "<td nowrap>" . say_numbercolor($resultG);
    $result0 = $result5 + $result9;
    echo "<td nowrap>" . say_numbercolor($result0 - $resultG);
    $total_amount += $amount;
    $sum_result1 += $result1;
    $sum_result5 += $result5;
    $sum_result7 += $result7;
    $sum_result8 += $result8;
    $sum_result9 += $result9;
    $sum_resultG += $resultG;
    $sum_result0 += $result0 - $resultG;
}
$sum_resultA = $sum_result0;
echo "<tr class=td3 align=right>";
echo "<td colspan=1 align=right><b>总计</b><td nowrap><b>" . $total_count . "</b><td nowrap><b>" . $total_amount . "</b><td nowrap><b>" . say_numbercolor($sum_result1) . "</b><td nowrap><b>" . say_numbercolor($sum_result5) . "</b><td nowrap><b>" . say_numbercolor($sum_result8) . "</b><td nowrap><b>" . say_numbercolor($sum_result9) . "</b><td nowrap><b>" . say_numbercolor($sum_resultG) . "</b><td nowrap><b>" . say_numbercolor($sum_result0) . "</b>";
echo "</td></tr>";
echo "</table>";
echo "<p><font color=red size=4><b>颁奖过帐完毕！&#12288;&#12288;&#12288;</b></font><input type=button value='回上一页' onclick=\"location.href='op.php?op=admin&fp=history&calc=1'\">&#12288;<input type=button value='重新颁奖' onclick=\"location.href='op.php?op=admin&fp=calc&id={$id}&mod=1'\"></p><br><br>";
$ui->footer();
?>